﻿using System.Windows;
using System.Windows.Controls;
using Jerry.DroidBuilder.ThemeEditor.Controls;

namespace Jerry.DroidBuilder.ThemeEditor
{
    public class LayoutContentTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var layoutInfo = item as LayoutInfo;
            var control = container as FrameworkElement;

            ComponentResourceKey componentKey = null;

            if (layoutInfo.LayoutType == "ScrollView")
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_ScrollContentTemplate");
            }
            else if (layoutInfo.LayoutType == "ImageView")
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_ImageViewTemplate");
            }
            else if (layoutInfo.LayoutType == "TextView")
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_TextViewTemplate");
            }
            else if (layoutInfo.LayoutType == "Button")
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_ButtonTemplate");
            }
            else if (layoutInfo.LayoutType == "RelativeLayout")
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_RelativeLayoutTemplate");
            }
            else
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_GenericContainerTemplate");
            }

            var resource = control.TryFindResource(componentKey) as DataTemplate;

            if (null == resource)
            {
                resource = base.SelectTemplate(item, container);
            }

            return resource;
        }
    }
}
